const state = {
  // 页面进来时优先使用旧数据, 如果没有再给默认空数组
  list: uni.getStorageSync('cart') || []
}
const mutations = {
  // 添加进购物车
  addCart (state, product) {
    // 添加逻辑不仅仅push 需要判断原本是否存在
    // 存在 就 number += 1, 否则才 push
    const prodFound = state.list.find(item=>item.goods_id === product.goods_id)
    if (prodFound) {
      prodFound.number += 1
    }else{
      state.list.push(product)
    }

    // 每当修改时备份数据
    uni.setStorageSync('cart', state.list)
  },
  // addProd(state, goods_id) {
  //   const prodFound = state.list.find(item=>item.goods_id === goods_id)
  //   prodFound.number++
  // },
  // minusProd(state, goods_id) {
  //   const prodFound = state.list.find(item=>item.goods_id === goods_id)
  //   prodFound.number--
  // }
  changeNumber(state, data) {
    const prodFound = state.list.find(item=>item.goods_id === data.goods_id)
    prodFound.number = data.number
  }
  
}
const actions = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}